<template>
  <el-table :data="entityData" highlight-current-row border>
    <el-table-column type="index" label="序号" sortable width="65" />
    <el-table-column prop="name" label="名称" show-overflow-tooltip />
    <el-table-column prop="size" label="大小" width="80" />
    <el-table-column
      prop="createTime"
      label="时间"
      width="100"
      :formatter="tableDateFormatter.formatTime"
    />
    <el-table-column fixed="right" label="操作" width="200">
      <template slot-scope="scope">
        <el-button
          type="primary"
          icon="el-icon-download"
          class="header-search_button"
          size="small"
          @click="download(scope.row)"
        >下载</el-button>
        <button-popover
          v-show="showDelete"
          class="header-search_button"
          placement="top"
          title="确定删除吗?"
          button-title="删除"
          button-size="small"
          button-type="primary"
          icon="el-icon-delete"
          @click-confirm="remove(scope.row.id)"
        />
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import ButtonPopover from '@/components/ButtonPopover'
export default {
  components: {
    ButtonPopover
  },
  props: {
    entityId: {
      type: String,
      default: '',
      required: true
    },
    showDelete: {
      type: Boolean,
      default: true,
      required: false
    }
  },
  data() {
    return {
      entityData: []
    }
  },
  watch: {
    entityId: function () {
      this.list()
    }
  },
  mounted: function () {
    this.list()
  },
  methods: {
    list() {
      if (this.entityId) {
        // 只有当entityId不为空时才发起查询
        this.$api.support.attachment.list({ entityId: this.entityId })
          .then(res => {
            this.entityData = res.data
          })
      }
    },
    remove(id) {
      this.$api.support.attachment.remove(id)
        .finally(() => {
          this.list()
        })
    },
    download(row) {
      this.$api.support.attachment.download(row.id, row.name)
    }
  }
}
</script>
